﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EasyERP_Model;


namespace EasyERP_DAL.Base
{
    /// <summary>
    /// 主代码数据访问类
    /// </summary>
    public class B_Major_DAL
    {
        #region 查询主代码信息
        /// <summary>
        /// 查询主代码信息
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回查询表</returns>
        public DataTable Get_B_Major(B_Major model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"SELECT major_cd, major_nm, remark FROM b_major
                WHERE major_cd LIKE N'%{model.major_cd}%'
                AND major_nm LIKE N'%{model.major_nm}%';");

            return DbHelperSQL.Query(sb.ToString()).Tables[0];
        }
        #endregion

        #region 添加主代码信息
        /// <summary>
        /// 添加主代码信息
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Insert_B_Major(B_Major model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"INSERT INTO b_major
                (major_cd, major_nm, remark, ins_user, ins_date, up_user, up_date)
                VALUES
                (N'{model.major_cd}', N'{model.major_nm}', N'{model.remark}', 
                N'{model.user_cd}', GETDATE(), N'{model.user_cd}', GETDATE());");

            return DbHelperSQL.ExecuteSql(sb.ToString()) > 0;
        }
        #endregion

        #region 删除主代码信息
        /// <summary>
        /// 删除主代码信息
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Delete_B_Major(B_Major model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"DELETE FROM dbo.b_major WHERE major_cd = N'{model.major_cd}';");

            return DbHelperSQL.ExecuteSql(sb.ToString()) > 0;
        }
        #endregion

        #region 修改主代码信息
        /// <summary>
        /// 修改主代码信息
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Update_B_Major(B_Major model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                UPDATE dbo.b_major
                SET major_nm = N'{model.major_nm}',
                    remark = N'{model.remark}',
                    up_user = N'{model.user_cd}',
                    up_date = GETDATE()
                WHERE major_cd = N'{model.major_cd}';
             ");

            return DbHelperSQL.ExecuteSql(sb.ToString()) > 0;
        }
        #endregion

        #region 查询数据是否存在
        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回True：存在；返回False：不存在</returns>
        public bool Exists(B_Major model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($"SELECT 1 FROM dbo.b_major WHERE major_cd = N'{model.major_cd}';");

            return DbHelperSQL.Exists(sb.ToString());
        }
        #endregion

        #region 删除主代码前查询是否有对应的子代码
        /// <summary>
        /// 删除主代码前查询是否有对应的子代码
        /// </summary>
        /// <param name="model">主代码实体对象</param>
        /// <returns>返回True：存在；返回False：不存在</returns>
        public bool ExistsMinor(B_Major model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                SELECT 1 FROM dbo.b_minor
                WHERE major_cd = N'{model.major_cd}';                
            ");

            return DbHelperSQL.Exists(sb.ToString());
        }
        #endregion
    }
}
